#define BOOST_TEST_MODULE testQueryHelper

#include <iostream>

#include <boost/test/unit_test.hpp>

#include "common/helpers/query_helper.h"

BOOST_AUTO_TEST_CASE(testQueryHelper01)
{
    const Common::QueryHelper query("login=eee&firstName=123+sadf");
    BOOST_CHECK(2 == query.keys().size());
    BOOST_CHECK("eee" == query.get("login"));
    BOOST_CHECK("www" == query.get("login2", "www"));
    BOOST_CHECK("123 sadf" == query.get("firstName", "www"));
}

BOOST_AUTO_TEST_CASE(testQueryHelper02)
{
    const Common::QueryHelper query("login=eee");
    BOOST_CHECK(1 == query.keys().size());
    BOOST_CHECK("eee" == query.get("login"));
    BOOST_CHECK("www" == query.get("login2", "www"));
    BOOST_CHECK("www" == query.get("firstName", "www"));
}

BOOST_AUTO_TEST_CASE(testQueryHelper03)
{
    const Common::QueryHelper query("");
    BOOST_CHECK(0 == query.keys().size());
    BOOST_CHECK("" == query.get("login"));
    BOOST_CHECK("www" == query.get("login2", "www"));
    BOOST_CHECK("www" == query.get("firstName", "www"));
}

BOOST_AUTO_TEST_CASE(testQueryHelper04)
{
    const auto queryParams = Common::queryMap(
        "login=eee+eee&firstName=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%21+++&r=1"
    );
    BOOST_CHECK(3 == queryParams.size());
    BOOST_CHECK("eee eee" == queryParams.at("login"));
    BOOST_CHECK(0 == queryParams.count("login2"));
    BOOST_CHECK("Привет!   " == queryParams.at("firstName"));
}

BOOST_AUTO_TEST_CASE(testQueryHelper05)
{
    const Common::QueryHelper query(
        "firstName=1%0D%0A2%0D%0A3++%0D%0A4"
    );
    BOOST_CHECK(1 == query.keys().size());
    BOOST_CHECK("1\r\n2\r\n3  \r\n4" == query.get("firstName", "www"));
}

BOOST_AUTO_TEST_CASE(testQueryHelper06)
{
    const Common::QueryHelper query(
        "isFirst=true&isSecond=+&isThird=ON&isForth=yEs&isFifth=falSe"
    );
    BOOST_CHECK(5 == query.keys().size());
    BOOST_CHECK(query.getBoolean("isFirst"));
    BOOST_CHECK(!query.getBoolean("isSecond"));
    BOOST_CHECK(query.getBoolean("isThird"));
    BOOST_CHECK(query.getBoolean("isForth"));
    BOOST_CHECK(!query.getBoolean("isFifth"));
    BOOST_CHECK(query.getBoolean("isLast", true));
}

BOOST_AUTO_TEST_CASE(testQueryHelper07)
{
    const Common::QueryHelper query(
        "isFirst=1&isSecond=+&isThird=3&isForth=44&isFifth=d4r"
    );
    BOOST_CHECK(5 == query.keys().size());
    BOOST_CHECK(1 == query.getInteger("isFirst"));
    BOOST_CHECK(0 == query.getInteger("isSecond"));
    BOOST_CHECK(3 == query.getInteger("isThird"));
    BOOST_CHECK(44 == query.getInteger("isForth"));
    BOOST_CHECK(5 == query.getInteger("isFifth", 5));
    BOOST_CHECK(5 == query.getInteger("isLast", 5));
}
